/*
* $Id: bigendian.c,v 1.15 2008/01/15 04:09:27 sunny Exp $
*/
/*******************************************************************************

       UBEC (Uniband Electronic Corp.)

       Project: U-NET01, Ubiquitous network platform

       File: bigebdian.c

       Version: 0.3.0

       Usage: Process the ending exchange

       Platform: U-NET01 DK with Keil 8051 C compiler

       Reference:

               Silicon Laboratories: C8051F124, C8051F340

               UBEC: UZ2400, UZ2410

       Note :

               Copyright (C) 2007 Uniband Electronic Corporation, All rights reserved

********************************************************************************/

#include "unet.h"

#if BIGENDIAN
void SWAPINT32(UINT32* Value){
	UINT32 Tmp;

		Tmp = *Value;

		((UINT8*)Value)[0] = ((UINT8*)&Tmp)[3];
		((UINT8*)Value)[1] = ((UINT8*)&Tmp)[2];
		((UINT8*)Value)[2] = ((UINT8*)&Tmp)[1];
		((UINT8*)Value)[3] = ((UINT8*)&Tmp)[0];
}

void SWAPINT16(UINT16* Value){
	UINT16 Tmp;

		Tmp = *Value;

		((UINT8*)Value)[0] = ((UINT8*)&Tmp)[1];
		((UINT8*)Value)[1] = ((UINT8*)&Tmp)[0];
}

//------------ Simon modify ---------------

void BE_MAC_DATA_PKT(UINT8 *DataPtr) {
	SWAPINT16(&((*(UNET_HEADER *)DataPtr).MHR.Header.val));
	SWAPINT16(&((*(UNET_HEADER *)DataPtr).MHR.DstPID));
	SWAPINT16(&((*(UNET_HEADER *)DataPtr).MHR.DstAddr));
	SWAPINT16(&((*(UNET_HEADER *)DataPtr).MHR.SrcPID));
	SWAPINT16(&((*(UNET_HEADER *)DataPtr).MHR.SrcAddr));
	SWAPINT16(&((*(UNET_HEADER *)DataPtr).DstAddr));
	SWAPINT16(&((*(UNET_HEADER *)DataPtr).SrcAddr));
//	SWAPINT16((UINT16 *)(&((*(UNET_HEADER *)DataPtr).UnetFrameCtrl)));
}

void BE_MAC_BCN_PKT(UINT8 *DataPtr) {
	SWAPINT16(&((*(MAC_BCN_PKT *)DataPtr).Header.val));
	SWAPINT16(&((*(MAC_BCN_PKT *)DataPtr).SrcPID));
	SWAPINT16(&((*(MAC_BCN_PKT *)DataPtr).SrcAddr));
	SWAPINT16(&((*(MAC_BCN_PKT *)DataPtr).SFS));
}

void BE_MCP_BCN_REQ(UINT8 *DataPtr) {
	SWAPINT16(&((*(MCP_BCN_REQ *)DataPtr).Header.val));
	SWAPINT16(&((*(MCP_BCN_REQ *)DataPtr).DstPID));
	SWAPINT16(&((*(MCP_BCN_REQ *)DataPtr).DstAddr));
}

void BE_MCP_ASSO_REQ(UINT8 *DataPtr) {
	SWAPINT16(&((*(MCP_ASSO_REQ *)DataPtr).Header.val));
	SWAPINT16(&((*(MCP_ASSO_REQ *)DataPtr).DstPID));
	SWAPINT16(&((*(MCP_ASSO_REQ *)DataPtr).DstAddr));
	SWAPINT16(&((*(MCP_ASSO_REQ *)DataPtr).SrcPID));
}

void BE_MCP_ASSO_RSP(UINT8 *DataPtr) {
	SWAPINT16(&((*(MCP_ASSO_RSP *)DataPtr).Header.val));
	SWAPINT16(&((*(MCP_ASSO_RSP *)DataPtr).DstPID));
	SWAPINT16(&((*(MCP_ASSO_RSP *)DataPtr).SrcPID));
	SWAPINT16(&((*(MCP_ASSO_RSP *)DataPtr).SrcAddr));
	SWAPINT16(&((*(MCP_ASSO_RSP *)DataPtr).ShortAddr));
}

void BE_MCP_DISASSO_NTF(UINT8 *DataPtr) {
	SWAPINT16(&((*(MCP_DISASSO_NTF *)DataPtr).Header.val));
	SWAPINT16(&((*(MCP_DISASSO_NTF *)DataPtr).DstPID));
	SWAPINT16(&((*(MCP_DISASSO_NTF *)DataPtr).SrcPID));
	SWAPINT16(&((*(MCP_DISASSO_NTF *)DataPtr).SrcAddr));
	SWAPINT16(&((*(MCP_DISASSO_NTF *)DataPtr).NewParent));
}

//	structtpye :
//				MAC_DATA_PKT 	= MT_DATA_IND
// 				MAC_BCN_PKT 	= MT_BCN_IND
//				MCP_BCN_REQ		= MT_BEACON_REQ
//				MCP_ASSO_REQ	= MT_ASSO_REQ
//				MCP_ASSO_RSP	= MT_ASSO_RSP
//				MCP_DISASSO_NTF	= MT_DISASSO_NTF
//				MCP_IPOP		= MT_IPOP
//				MCP_CBACK		= MT_CBACK
//				MCP_PBACK		= MT_PBACK
void Endian_Modify(UINT8 *DataPtr,UINT8 mactype,UINT8 unettype) {
	if(mactype != 0xFF)
		switch(mactype){
			case MT_DATA_IND:
				BE_MAC_DATA_PKT(DataPtr);
				break;
			case MT_BCN_IND:
				BE_MAC_BCN_PKT(DataPtr);
				break;
			case MT_BEACON_REQ:
				BE_MCP_BCN_REQ(DataPtr);
				break;
			case MT_ASSO_REQ:
				BE_MCP_ASSO_REQ(DataPtr);
				break;
			case MT_ASSO_RSP:
				BE_MCP_ASSO_RSP(DataPtr);
				break;
			case MT_DISASSO_NTF:
				BE_MCP_DISASSO_NTF(DataPtr);
			default:
				break;
		}
	if(unettype != 0xFF)
		switch(unettype){	//for unet
			case UNET_JOIN_REPORT:
			//	SWAPINT32(&((*(UNET_JOINREPORT_FRAME *)DataPtr).JoinChildMACAddress[0]));
			//	SWAPINT32(&((*(UNET_JOINREPORT_FRAME *)DataPtr).JoinChildMACAddress[1]));
				SWAPINT16(&((*(UNET_JOINREPORT_FRAME *)DataPtr).JoinChildNWKAddress));
				break;
			case UNET_KICK_CMD:
				SWAPINT16(&((*(UNET_KICK_FRAME *)DataPtr).KickChildAddress));
				SWAPINT16(&((*(UNET_KICK_FRAME *)DataPtr).NewParent));
				break;
			case UNET_LEAVE_REPORT:
				SWAPINT16(&((*(UNET_LEAVE_FRAME *)DataPtr).LeaveChildAddress));
				break;
#if 0
			case UNET_CS:
				SWAPINT16(&((*(UNET_LEAVE_FRAME *)DataPtr).LeaveChildAddress));
				break;
#endif
			case UNET_RSP_MAC_ADDR:
			//	SWAPINT32(&((*(UNET_RSP_MA_FRAME *)DataPtr).RspMACAddress[0]));
			//	SWAPINT32(&((*(UNET_RSP_MA_FRAME *)DataPtr).RspMACAddress[1]));
				break;
			case UNET_MAC_ROUTE:
			//	SWAPINT32(&((*(UNET_MAC_ROUTE_FRAME *)DataPtr).DstMACAddress[0]));
			//	SWAPINT32(&((*(UNET_MAC_ROUTE_FRAME *)DataPtr).DstMACAddress[1]));
				break;
			case UNET_LOCAL_QUERY:
				SWAPINT32(&((*(UNET_LOCAL_QUERY_FRAME *)DataPtr).Interval));
				break;
#if DEBUG_FUNC
			case UNET_DBG_NODE_INFO_RSP:
				SWAPINT16(&((*(UNET_NODE_INFO_FRAME *)DataPtr).parent_nwk_address));
				SWAPINT16(&((*(UNET_NODE_INFO_FRAME *)DataPtr).my_nwk_address));
				break;
#endif
			case UNET_LOCAL_TALK:
			case UNET_RESET:
			case UNET_DATA:
			case UNET_ASK_MAC_ADDR:

			default:
				break;
		}
}
#endif // #if BIGENDIAN
